package com.java.spring.simple.module.admin.auth.controller;

import java.net.URL;
import java.util.List;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.java.spring.simple.common.core.constant.Constants;
import com.java.spring.simple.common.core.domain.R;
import com.java.spring.simple.common.core.domain.model.RegisterBody;
import com.java.spring.simple.common.core.domain.model.SmsLoginBody;
import com.java.spring.simple.common.core.utils.MapstructUtils;
import com.java.spring.simple.common.core.utils.StreamUtils;
import com.java.spring.simple.common.core.utils.StringUtils;
import com.java.spring.simple.common.satoken.utils.LoginHelper;
import com.java.spring.simple.common.tenant.helper.TenantHelper;
import com.java.spring.simple.module.admin.auth.service.AuthLoginService;
import com.java.spring.simple.module.admin.auth.service.RegisterService;
import com.java.spring.simple.module.infrastructure.domin.bo.AuthLoginBo;
import com.java.spring.simple.module.infrastructure.domin.bo.SysTenantBo;
import com.java.spring.simple.module.infrastructure.domin.vo.LoginTenantVo;
import com.java.spring.simple.module.infrastructure.domin.vo.LoginVo;
import com.java.spring.simple.module.infrastructure.domin.vo.SysTenantVo;
import com.java.spring.simple.module.infrastructure.domin.vo.TenantListVo;
import com.java.spring.simple.module.infrastructure.service.tenant.ISysTenantService;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.collection.CollUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;

/**
 * 认证模块
 * 
 * @author Luo wang
 */
@SaIgnore
@RestController
@RequestMapping("/admin/auth")
public class AuthLoginController {

    @Resource
    private AuthLoginService loginService;

    @Resource
    private RegisterService registerService;

    @Resource
    private ISysTenantService tenantService;

    /**
     * 登录
     *
     * @param authLoginBo 登录信息
     * @return 结果
     **/
    @PostMapping("/login")
    public R<LoginVo> login(@Validated @RequestBody AuthLoginBo authLoginBo) {
        authLoginBo.setTenantId(Constants.TENANT_ID);
        LoginVo loginVo = new LoginVo();
        String token = loginService.login(authLoginBo.getTenantId(), authLoginBo.getUsername(),
            authLoginBo.getPassword(), authLoginBo.getCode(), authLoginBo.getUuid());
        loginVo.setToken(token);
        loginVo.setUserInfo(LoginHelper.getLoginUser());
        return R.of(loginVo);
    }

    /**
     * 短信登录
     * 
     * @param body 短信信息
     * @return 结果
     **/
    @PostMapping("/smsLogin")
    public R<LoginVo> smsLogin(@Validated @RequestBody SmsLoginBody body) {
        LoginVo loginVo = new LoginVo();
        // 生成令牌
        String token = loginService.smsLogin(body.getTenantId(), body.getPhonenumber(), body.getSmsCode());
        loginVo.setToken(token);
        return R.of(loginVo);
    }

    /**
     * 退出登录
     *
     * @return 结果
     **/
    @PostMapping("/logout")
    public R<Void> logout() {
        loginService.logout();
        return R.of("退出成功");
    }

    /**
     * 用户注册
     */
    @PostMapping("/register")
    public R<Void> register(@Validated @RequestBody RegisterBody user, HttpServletRequest request) {
        String domainName = request.getServerName();
        user.setDomainName(domainName);
        registerService.register(user);
        return R.of();
    }

    /**
     * 重置密码
     */
    @PostMapping("/reset/password")
    @SaIgnore
    public R<Void> resetPassWord(@Validated @RequestBody RegisterBody user) {
        registerService.resetPassWord(user);
        return R.of();
    }

    /**
     * 登录页面租户下拉框
     *
     * @return 租户列表
     */
    @GetMapping("/tenant/list")
    public R<LoginTenantVo> tenantList(HttpServletRequest request) throws Exception {
        List<SysTenantVo> tenantList = tenantService.queryList(new SysTenantBo());
        List<TenantListVo> voList = MapstructUtils.convert(tenantList, TenantListVo.class);
        // 获取域名
        String host = new URL(request.getRequestURL().toString()).getHost();
        // 根据域名进行筛选
        List<TenantListVo> list = StreamUtils.filter(voList, vo -> StringUtils.equals(vo.getDomain(), host));
        // 返回对象
        LoginTenantVo vo = new LoginTenantVo();
        vo.setVoList(CollUtil.isNotEmpty(list) ? list : voList);
        vo.setTenantEnabled(TenantHelper.isEnable());
        return R.of(vo);
    }
}
